home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / pixtest.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  35KB  |  1,319 lines

  1. /*
  2.  * (c) Copyright 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <GL/glx.h>
  39. #include <stdio.h>
  40. #include <math.h>
  41. #include <string.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <unistd.h>
  46. #include <stdlib.h>
  47. #include <X11/keysym.h>
  48. #include <malloc.h>
  49.  
  50. static int RGB_SB_attributes[] = {
  51.     GLX_RGBA,
  52.     GLX_RED_SIZE, 1,
  53.     GLX_GREEN_SIZE, 1,
  54.     GLX_BLUE_SIZE, 1,
  55.     GLX_DEPTH_SIZE, 1,
  56.     GLX_STENCIL_SIZE, 1,
  57.     None,
  58. };
  59.  
  60. static int RGB_DB_attributes[] = {
  61.     GLX_RGBA,
  62.     GLX_RED_SIZE, 1,
  63.     GLX_GREEN_SIZE, 1,
  64.     GLX_BLUE_SIZE, 1,
  65.     GLX_DOUBLEBUFFER,
  66.     GLX_DEPTH_SIZE, 1,
  67.     GLX_STENCIL_SIZE, 1,
  68.     None,
  69. };
  70.  
  71. static int CI_SB_attributes[] = {
  72.     GLX_DEPTH_SIZE, 1,
  73.     GLX_STENCIL_SIZE, 1,
  74.     None,
  75. };
  76.  
  77. static int CI_DB_attributes[] = {
  78.     GLX_DOUBLEBUFFER,
  79.     GLX_DEPTH_SIZE, 1,
  80.     GLX_STENCIL_SIZE, 1,
  81.     None,
  82. };
  83.  
  84. Display *dpy;
  85. Window window;
  86. GLfloat *fltImage;      /* GL_RGBA, GL_FLOAT */
  87. GLubyte *bitmapImage;
  88. GLint imageWidth, imageHeight;
  89. GLfloat zoomx, zoomy;
  90. void *newimage;
  91. GLenum type, format, copyFormat;
  92. GLint centerx, centery;
  93. GLint scissor;
  94. GLint biasing;
  95. GLint mapping;
  96. GLint test;
  97. GLint rgbMode;
  98. GLint alphatst;
  99. GLint swapBytes;
  100. GLfloat quant4[4];
  101. void *tempbuf;
  102. GLint windowWidth, windowHeight;
  103. GLint alignShift;
  104. GLboolean dlisted;
  105. GLboolean doubleBuffer;
  106. GLuint dlistnum;
  107. GLuint maxindex;
  108.  
  109. #define TEST_DRAW       0
  110. #define TEST_READ       1
  111. #define TEST_COPY       2
  112.  
  113. typedef struct _rawImageRec {
  114.     unsigned short imagic;
  115.     unsigned short type;
  116.     unsigned short dim;
  117.     unsigned short sizeX, sizeY, sizeZ;
  118.     unsigned long min, max;
  119.     unsigned long wasteBytes;
  120.     char name[80];
  121.     unsigned long colorMap;
  122.     FILE *file;
  123.     unsigned char *tmp, *tmpR, *tmpG, *tmpB;
  124.     unsigned long rleEnd;
  125.     unsigned long *rowStart;
  126.     long *rowSize;
  127. } rawImageRec;
  128.  
  129. typedef struct image {
  130.     long sizeX, sizeY;
  131.     unsigned char *data;
  132. } Image;
  133.  
  134. Image *image;
  135.  
  136. static rawImageRec *RawImageOpen(char *fileName)
  137. {
  138.     rawImageRec *raw;
  139.     long x;
  140.  
  141.     raw = (rawImageRec *)malloc(sizeof(rawImageRec));
  142.     if (raw == NULL) {
  143.         fprintf(stderr, "Out of memory!\n");
  144.         exit(1);
  145.     }
  146.     if ((raw->file = fopen(fileName, "rb")) == NULL) {
  147.         perror(fileName);
  148.         exit(1);
  149.     }
  150.  
  151.     fread(raw, 1, 12, raw->file );
  152.  
  153.     raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
  154.     raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
  155.     raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
  156.     raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
  157.     if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
  158.             raw->tmpB == NULL) {
  159.         fprintf(stderr, "Out of memory!\n");
  160.         exit(1);
  161.     }
  162.     if ((raw->type & 0xFF00) == 0x0100) {
  163.         x = raw->sizeY * raw->sizeZ * sizeof(long);
  164.         raw->rowStart = (unsigned long *)malloc(x);
  165.         raw->rowSize = (long *)malloc(x);
  166.         if (raw->rowStart == NULL || raw->rowSize == NULL) {
  167.             fprintf(stderr, "Out of memory!\n");
  168.             exit(1);
  169.         }
  170.         raw->rleEnd = 512 + (2 * x);
  171.  
  172.         fseek(raw->file, 512L, SEEK_SET);
  173.         fread(raw->rowStart, 1, x, raw->file);
  174.         fread(raw->rowSize, 1, x, raw->file);
  175.     }
  176.     return raw;
  177. }
  178.  
  179. static void RawImageClose(rawImageRec *raw)
  180. {
  181.     fclose(raw->file);
  182.     free(raw->tmp);
  183.     free(raw->tmpR);
  184.     free(raw->tmpG);
  185.     free(raw->tmpB);
  186.     free(raw);
  187. }
  188.  
  189. static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, long y, long z)
  190. {
  191.     unsigned char *iPtr, *oPtr, pixel;
  192.     long count;
  193.  
  194.     if ((raw->type & 0xFF00) == 0x0100) {
  195.         fseek(raw->file, raw->rowStart[y+z*raw->sizeY], SEEK_SET);
  196.         fread(raw->tmp, 1, raw->rowSize[y+z*raw->sizeY], raw->file);
  197.  
  198.         iPtr = raw->tmp;
  199.         oPtr = buf;
  200.         while (1) {
  201.             pixel = *iPtr++;
  202.             count = (long)(pixel & 0x7f);
  203.             if (!count)
  204.                 return;
  205.             if (pixel & 0x80) {
  206.                 while (count--) {
  207.                     *oPtr++ = *iPtr++;
  208.                 }
  209.             } else {
  210.                 pixel = *iPtr++;
  211.                 while (count--) {
  212.                     *oPtr++ = pixel;
  213.                 }
  214.             }
  215.         }
  216.     } else {
  217.         fseek(raw->file, 512L+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
  218.                 SEEK_SET);
  219.         fread(buf, 1, raw->sizeX, raw->file);
  220.     }
  221. }
  222.  
  223. static void RawImageGetData(rawImageRec *raw, Image *final)
  224. {
  225.     long i, j;
  226.     unsigned char *ptr;
  227.  
  228.     final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  229.     if (final->data == NULL) {
  230.         fprintf(stderr, "Out of memory!\n");
  231.         exit(1);
  232.     }
  233.  
  234.     ptr = final->data;
  235.     for (i=0; i<raw->sizeY; i++) {
  236.         RawImageGetRow(raw, raw->tmpR, i, 0);
  237.         RawImageGetRow(raw, raw->tmpG, i, 1);
  238.         RawImageGetRow(raw, raw->tmpB, i, 2);
  239.         for (j=0; j<raw->sizeX; j++) {
  240.             *ptr++ = *(raw->tmpR + j);
  241.             *ptr++ = *(raw->tmpG + j);
  242.             *ptr++ = *(raw->tmpB + j);
  243.         }
  244.     }
  245. }
  246.  
  247. Image *ImageLoad(char *fileName)
  248. {
  249.     rawImageRec *raw;
  250.     Image *final;
  251.  
  252.     raw = RawImageOpen(fileName);
  253.     final = (Image *)malloc(sizeof(Image));
  254.     if (final == NULL) {
  255.         fprintf(stderr, "Out of memory!\n");
  256.         exit(1);
  257.     }
  258.     final->sizeX = raw->sizeX;
  259.     final->sizeY = raw->sizeY;
  260.     RawImageGetData(raw, final);
  261.     RawImageClose(raw);
  262.     return final;
  263. }
  264.  
  265. void createFloatImage(void)
  266. {
  267.     GLubyte *rawData;
  268.     GLint x,y;
  269.     GLfloat *finalImage;
  270.     GLfloat alpha;
  271.  
  272.     rawData = image->data;
  273.     fltImage = malloc(imageWidth * imageHeight * sizeof(GLfloat) * 4);
  274.     tempbuf = malloc(imageWidth * imageHeight * sizeof(GLdouble) * 4 + 3);
  275.     finalImage = fltImage;
  276.     for (y = 0; y < imageHeight; y++) {
  277.         for (x = 0; x < imageWidth; x++) {
  278.             *finalImage++ = *rawData++ / 255.0;
  279.             alpha = *finalImage++ = *rawData++ / 255.0;
  280.             *finalImage++ = *rawData++ / 255.0;
  281.             *finalImage++ = alpha;
  282.         }
  283.     }
  284. }
  285.  
  286. static void Init(void)
  287. {
  288.     int i;
  289.     GLfloat array[8];
  290.  
  291.     imageWidth = image->sizeX;
  292.     imageHeight = image->sizeY;
  293.     centerx = image->sizeX;
  294.     centery = image->sizeY;
  295.  
  296.     createFloatImage();
  297.     newimage = image->data;
  298.  
  299.     for (i=0; i<4; i++) {
  300.         quant4[i] = (2*i+1)/8.0;
  301.     }
  302.  
  303.     glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 4, quant4);
  304.     glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 4, quant4);
  305.     glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 4, quant4);
  306.     glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 4, quant4);
  307.     for (i=0; i<8; i++) {
  308.         array[i] = (i&1) ? 1 : 0;
  309.     }
  310.     glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 8, array);
  311.     for (i=0; i<8; i++) {
  312.         array[i] = (i&2) ? 1 : 0;
  313.     }
  314.     glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 8, array);
  315.     for (i=0; i<8; i++) {
  316.         array[i] = (i&4) ? 1 : 0;
  317.     }
  318.     glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 8, array);
  319.     array[0] = 0;
  320.     array[1] = 1;
  321.     glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 2, array);
  322.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  323.     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  324.     glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
  325.     glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
  326.     glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
  327.     glPixelStorei(GL_PACK_LSB_FIRST, GL_TRUE);
  328.     glScissor(0,0,windowWidth,windowHeight);
  329.     glEnable(GL_SCISSOR_TEST);
  330.     glColor3f(1.0, 1.0, 1.0);
  331.  
  332.     dlistnum = glGenLists(1);
  333. }
  334.  
  335. static void Redraw(void)
  336. {
  337.     GLubyte *s;
  338.     int i,j;
  339.     static int lasttest = TEST_DRAW;
  340.  
  341.     glViewport(0, 0, 4*imageWidth, 2*imageHeight);
  342.     glMatrixMode(GL_PROJECTION);
  343.     glLoadIdentity();
  344.     glOrtho(0, 4*imageWidth, 0, 2*imageHeight, -1, 1);
  345.     glMatrixMode(GL_MODELVIEW);
  346.     glScissor(0,0,windowWidth,windowHeight);
  347.  
  348.     glClearColor(0.0, 0.0, 0.0, 0.0);
  349.  
  350.     if (alphatst && (type == GL_BITMAP ||
  351.             format == GL_ALPHA || format == GL_LUMINANCE_ALPHA)) {
  352.         glClearColor(0.3, 0.3, 0.3, 0.0);
  353.     }
  354.  
  355.     glClear(GL_COLOR_BUFFER_BIT);
  356.  
  357.     if (test != lasttest) {
  358.         lasttest = test;
  359.         switch(test) {
  360.           case TEST_DRAW:
  361.             printf("Test = DrawPixels\n");
  362.             break;
  363.           case TEST_COPY:
  364.             printf("Test = CopyPixels\n");
  365.             break;
  366.           case TEST_READ:
  367.             printf("Test = ReadPixels\n");
  368.             break;
  369.         }
  370.     }
  371.  
  372.     if (format == GL_DEPTH_COMPONENT) {
  373.         glDepthFunc(GL_ALWAYS);
  374.         glClearDepth(0.5);
  375.         glClear(GL_DEPTH_BUFFER_BIT);
  376.         glEnable(GL_DEPTH_TEST);
  377.     }
  378.     if (format == GL_STENCIL_INDEX) {
  379.         glEnable(GL_STENCIL_TEST);
  380.         glClearStencil(0);
  381.         glClear(GL_STENCIL_BUFFER_BIT);
  382.         glStencilFunc(GL_ALWAYS, 1, 255);
  383.     }
  384.  
  385.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  386.  
  387.     if (test == TEST_READ || test == TEST_COPY) {
  388.         glRasterPos3f(0,0,-1);
  389.         glBitmap(0,0,0,0,centerx - imageWidth/2.0,
  390.             centery - imageHeight/2.0,NULL);
  391.         glDrawPixels(imageWidth, imageHeight, format, type,
  392.             newimage);
  393.     }
  394.  
  395.     glPixelZoom(zoomx, zoomy);
  396.  
  397.     if (format == GL_DEPTH_COMPONENT) {
  398.         glDepthFunc(GL_GEQUAL);
  399.     }
  400.     if (biasing) {
  401.         glPixelTransferf(GL_RED_SCALE, -1);
  402.         glPixelTransferf(GL_RED_BIAS, 1);
  403.         glPixelTransferf(GL_GREEN_SCALE, -1);
  404.         glPixelTransferf(GL_GREEN_BIAS, 1);
  405.         glPixelTransferf(GL_BLUE_SCALE, -1);
  406.         glPixelTransferf(GL_BLUE_BIAS, 1);
  407.     }
  408.     if (mapping) {
  409.         glPixelTransferi(GL_MAP_COLOR, GL_TRUE);
  410.     }
  411.     if (scissor) {
  412.         glScissor(imageWidth/2, imageHeight/2,
  413.                 imageWidth, imageHeight);
  414.     }
  415.     if (alphatst) {
  416.         glEnable(GL_ALPHA_TEST);
  417.         glAlphaFunc(GL_GREATER, 0.5);
  418.     }
  419.  
  420.     if (test == TEST_DRAW) {
  421.     if (dlisted) {
  422.         glNewList(dlistnum, GL_COMPILE);
  423.     }
  424.         glRasterPos3f(0,0,-1);
  425.         glBitmap(0,0,0,0,centerx - zoomx * imageWidth / 2,
  426.             centery - zoomy * imageHeight / 2,NULL);
  427.         glDrawPixels(imageWidth, imageHeight, format, type,
  428.             newimage);
  429.     if (dlisted) {
  430.         glEndList();
  431.         glCallList(dlistnum);
  432.     }
  433.     } else if (test == TEST_READ) {
  434.         glReadPixels(centerx - (1+imageWidth)/2,
  435.             centery - (1+imageHeight)/2, imageWidth,
  436.             imageHeight, format, type, ((GLubyte *) tempbuf) + alignShift);
  437.     } else if (test == TEST_COPY) {
  438.         glRasterPos3f(0,0,-1);
  439.         glBitmap(0,0,0,0,imageWidth - zoomx * imageWidth / 2,
  440.             imageHeight - zoomy * imageHeight / 2,NULL);
  441.         glCopyPixels(centerx - (1+imageWidth) / 2,
  442.             centery - (1+imageHeight) / 2, imageWidth,
  443.             imageHeight, copyFormat);
  444.     }
  445.  
  446.     if (biasing) {
  447.         glPixelTransferf(GL_RED_SCALE, 1);
  448.         glPixelTransferf(GL_RED_BIAS, 0);
  449.         glPixelTransferf(GL_GREEN_SCALE, 1);
  450.         glPixelTransferf(GL_GREEN_BIAS, 0);
  451.         glPixelTransferf(GL_BLUE_SCALE, 1);
  452.         glPixelTransferf(GL_BLUE_BIAS, 0);
  453.     }
  454.     if (mapping) {
  455.         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
  456.     }
  457.  
  458.     if (test == TEST_READ) {
  459.         glRasterPos3f(0,0,-1);
  460.         glBitmap(0,0,0,0,imageWidth - zoomx * imageWidth / 2,
  461.             imageHeight - zoomy * imageHeight / 2,NULL);
  462.         glDrawPixels(imageWidth, imageHeight, format, type,
  463.             ((GLubyte *) tempbuf) + alignShift);
  464.     }
  465.     if (format == GL_STENCIL_INDEX) {
  466.         if (type == GL_BITMAP) {
  467.             glStencilFunc(GL_EQUAL, 1, 1);
  468.         } else if (type == GL_BYTE) {
  469.         glStencilFunc(GL_LEQUAL, (maxindex/4)+1, maxindex);
  470.     } else {
  471.             glStencilFunc(GL_LEQUAL, (maxindex/2)+1, maxindex);
  472.         }
  473.         glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  474.         glRecti(0, 0, 4*imageWidth, 2*imageHeight);
  475.     }
  476.     glFlush();
  477.  
  478.     if (format == GL_DEPTH_COMPONENT) {
  479.         glDisable(GL_DEPTH_TEST);
  480.     }
  481.     if (format == GL_STENCIL_INDEX) {
  482.         glDisable(GL_STENCIL_TEST);
  483.     }
  484.     if (scissor) {
  485.         glScissor(0,0,windowWidth,windowHeight);
  486.     }
  487.     if (alphatst) {
  488.         glDisable(GL_ALPHA_TEST);
  489.     }
  490.  
  491.     glPixelZoom(1.0, 1.0);
  492.  
  493.     if (doubleBuffer) {
  494.     glXSwapBuffers(dpy, window);
  495.     }
  496. }
  497.  
  498. GLboolean okCombo(void)
  499. {
  500.     if (rgbMode && type == GL_BITMAP && format != GL_COLOR_INDEX &&
  501.             format != GL_STENCIL_INDEX) {
  502.         return GL_FALSE;
  503.     }
  504.     if (test == TEST_READ && rgbMode && format == GL_COLOR_INDEX) {
  505.         return GL_FALSE;
  506.     }
  507.     return GL_TRUE;
  508. }
  509.  
  510. void makeNewImage(void)
  511. {
  512.     int components;
  513.     int bytesPerComp;
  514.     GLfloat *myimage;
  515.     int i,j,k;
  516.     int index;
  517.     GLfloat indexscale;
  518.     int imageSize;
  519.  
  520.     if (swapBytes) {
  521.         if (type == GL_BITMAP) {
  522.             printf("MSB_FIRST, ");
  523.         } else {
  524.             printf("SWAP_BYTES, ");
  525.         }
  526.     } else {
  527.         if (type == GL_BITMAP) {
  528.             printf("LSB_FIRST, ");
  529.         }
  530.     }
  531.     if (alignShift) {
  532.         printf("(%d*), ", alignShift);
  533.     }
  534.     switch(type) {
  535.       case GL_BYTE:
  536.         bytesPerComp = 1;
  537.         printf("Type: GL_BYTE, ");
  538.         break;
  539.       case GL_UNSIGNED_BYTE:
  540.         bytesPerComp = 1;
  541.         printf("Type: GL_UNSIGNED_BYTE, ");
  542.         break;
  543.       case GL_INT:
  544.         bytesPerComp = 4;
  545.         printf("Type: GL_INT, ");
  546.         break;
  547.       case GL_UNSIGNED_INT:
  548.         bytesPerComp = 4;
  549.         printf("Type: GL_UNSIGNED_INT, ");
  550.         break;
  551.       case GL_BITMAP:
  552.         printf("Type: GL_BITMAP, ");
  553.         break;
  554.       case GL_FLOAT:
  555.         bytesPerComp = 4;
  556.         printf("Type: GL_FLOAT, ");
  557.         break;
  558.       case GL_UNSIGNED_SHORT:
  559.         bytesPerComp = 2;
  560.         printf("Type: GL_UNSIGNED_SHORT, ");
  561.         break;
  562.       case GL_SHORT:
  563.         bytesPerComp = 2;
  564.         printf("Type: GL_SHORT, ");
  565.         break;
  566.     }
  567.     copyFormat = GL_COLOR;
  568.     index = 0;
  569.     switch(format) {
  570.       case GL_RGB:
  571.         components = 3;
  572.         printf("Format: GL_RGB\n");
  573.         break;
  574.       case GL_RED:
  575.         components = 1;
  576.         printf("Format: GL_RED\n");
  577.         break;
  578.       case GL_GREEN:
  579.         components = 1;
  580.         printf("Format: GL_GREEN\n");
  581.         break;
  582.       case GL_BLUE:
  583.         components = 1;
  584.         printf("Format: GL_BLUE\n");
  585.         break;
  586.       case GL_ALPHA:
  587.         components = 1;
  588.         printf("Format: GL_ALPHA\n");
  589.         break;
  590.       case GL_LUMINANCE_ALPHA:
  591.         components = 2;
  592.         printf("Format: GL_LUMINANCE_ALPHA\n");
  593.         break;
  594.       case GL_LUMINANCE:
  595.         components = 1;
  596.         printf("Format: GL_LUMINANCE\n");
  597.         break;
  598.       case GL_RGBA:
  599.         components = 4;
  600.         printf("Format: GL_RGBA\n");
  601.         break;
  602.       case GL_COLOR_INDEX:
  603.         components = 1;
  604.         index = 1;
  605.         printf("Format: GL_COLOR_INDEX\n");
  606.         break;
  607.       case GL_DEPTH_COMPONENT:
  608.         components = 1;
  609.         copyFormat = GL_DEPTH;
  610.         printf("Format: GL_DEPTH_COMPONENT\n");
  611.         break;
  612.       case GL_STENCIL_INDEX:
  613.         components = 1;
  614.         index = 1;
  615.         copyFormat = GL_STENCIL;
  616.         printf("Format: GL_STENCIL_INDEX\n");
  617.         break;
  618.     }
  619.     if (index) {
  620.     GLint bufferbits, typebits;
  621.     switch (format) {
  622.       case GL_COLOR_INDEX:
  623.         glGetIntegerv(GL_INDEX_BITS, &bufferbits);
  624.         break;
  625.       case GL_STENCIL_INDEX:
  626.         glGetIntegerv(GL_STENCIL_BITS, &bufferbits);
  627.         break;
  628.     }
  629.     maxindex = (bufferbits > 0) ? ~(~1 << bufferbits-1) : 0;
  630.     switch (type) {
  631.        case GL_BITMAP:
  632.        case GL_FLOAT:
  633.          typebits = bufferbits;
  634.          break;
  635.        case GL_BYTE:
  636.          typebits = 8*sizeof(GLbyte) - 1;
  637.          break;
  638.        case GL_UNSIGNED_BYTE:
  639.          typebits = 8*sizeof(GLubyte);
  640.          break;
  641.        case GL_INT:
  642.          typebits = 8*sizeof(GLint) - 1;
  643.          break;
  644.        case GL_UNSIGNED_INT:
  645.          typebits = 8*sizeof(GLuint);
  646.          break;
  647.        case GL_SHORT:
  648.          typebits = 8*sizeof(GLshort) - 1;
  649.          break;
  650.        case GL_UNSIGNED_SHORT:
  651.          typebits = 8*sizeof(GLushort);
  652.          break;
  653.     }
  654.     if (typebits < bufferbits) {
  655.         indexscale = (GLfloat) pow(2.0, (double) typebits) - 1.0;
  656.     } else {
  657.         indexscale = (GLfloat) pow(2.0, (double) bufferbits) - 1.0;
  658.     }
  659.     } else {
  660.     maxindex = 0;
  661.     indexscale = 0;
  662.     }
  663.  
  664.     /* Build the new image */
  665.     free((void *) ((int) (newimage) & 0xfffffffc));
  666.  
  667.     if (type != GL_BITMAP) {
  668.         imageSize = imageWidth*imageHeight*components*bytesPerComp;
  669.     } else {
  670.         imageSize = ((imageWidth + 7)/8)*imageHeight;
  671.     }
  672.     newimage = malloc(imageSize + 3);
  673.  
  674.     myimage = fltImage;
  675.     if (format == GL_GREEN) myimage += 1;
  676.     else if (format == GL_BLUE || format == GL_LUMINANCE ||
  677.             format == GL_LUMINANCE_ALPHA) myimage += 2;
  678.     else if (format == GL_ALPHA) myimage += 3;
  679.     if (type == GL_BITMAP) {
  680.         GLubyte *data;
  681.         GLint bit;
  682.         GLubyte byte;
  683.  
  684.         data = newimage;
  685.         for (i=0; i<imageHeight; i++) {
  686.             bit = 0;
  687.             byte = 0;
  688.             for (j=0; j<imageWidth; j++) {
  689.                 if (*myimage++ > 0.5) {
  690.                     if (swapBytes) {
  691.                         byte |= 1<<(7-bit);
  692.                     } else {
  693.                         byte |= 1<<bit;
  694.                     }
  695.                 }
  696.                 bit++;
  697.                 if (bit == 8) {
  698.                     bit = 0;
  699.                     *data++ = byte;
  700.                     byte = 0;
  701.                 }
  702.                 myimage += 3;
  703.             }
  704.             if (bit) {
  705.                 *data++ = byte;
  706.             }
  707.         }
  708.     } else if (type == GL_UNSIGNED_BYTE) {
  709.         GLubyte *data;
  710.  
  711.         data = newimage;
  712.         for (i=0; i<imageHeight; i++) {
  713.             for (j=0; j<imageWidth; j++) {
  714.                 for (k=0; k<components; k++) {
  715.             if (index) {
  716.             *data++ = *myimage++ * indexscale;
  717.             } else {
  718.             *data++ = *myimage++ * 255.0;
  719.             }
  720.                 }
  721.                 myimage+=(4-components);
  722.             }
  723.         }
  724.     } else if (type == GL_BYTE) {
  725.         GLbyte *data;
  726.  
  727.         data = newimage;
  728.         for (i=0; i<imageHeight; i++) {
  729.             for (j=0; j<imageWidth; j++) {
  730.                 for (k=0; k<components; k++) {
  731.                     if (index) {
  732.                         *data++ = *myimage++ * indexscale;
  733.                     } else {
  734.                         *data++ = *myimage++ * 127.0;
  735.                     }
  736.                 }
  737.                 myimage+=(4-components);
  738.             }
  739.         }
  740.     } else if (type == GL_UNSIGNED_SHORT) {
  741.         GLushort *data;
  742.         GLubyte *temp1, *temp2;
  743.         GLushort answer;
  744.  
  745.         data = newimage;
  746.         for (i=0; i<imageHeight; i++) {
  747.             for (j=0; j<imageWidth; j++) {
  748.                 for (k=0; k<components; k++) {
  749.                     if (index) {
  750.                         answer = *myimage++ * indexscale;
  751.                     } else {
  752.                         answer = *myimage++ * (GLfloat) 65535.0;
  753.                     }
  754.                     if (swapBytes) {
  755.                         temp2 = (GLubyte *) &answer;
  756.                         temp1 = (GLubyte *) data;
  757.                         temp1[0] = temp2[1];
  758.                         temp1[1] = temp2[0];
  759.                     } else {
  760.                         data[0] = answer;
  761.                     }
  762.                     data++;
  763.                 }
  764.                 myimage+=(4-components);
  765.             }
  766.         }
  767.     } else if (type == GL_SHORT) {
  768.         GLshort *data;
  769.         GLubyte *temp1, *temp2;
  770.         GLshort answer;
  771.  
  772.         data = newimage;
  773.         for (i=0; i<imageHeight; i++) {
  774.             for (j=0; j<imageWidth; j++) {
  775.                 for (k=0; k<components; k++) {
  776.                     if (index) {
  777.                         answer = *myimage++ * indexscale;
  778.                     } else {
  779.                         answer = *myimage++ * (GLfloat) 32767.0;
  780.                     }
  781.                     if (swapBytes) {
  782.                         temp2 = (GLubyte *) &answer;
  783.                         temp1 = (GLubyte *) data;
  784.                         temp1[0] = temp2[1];
  785.                         temp1[1] = temp2[0];
  786.                     } else {
  787.                         data[0] = answer;
  788.                     }
  789.                     data++;
  790.                 }
  791.                 myimage+=(4-components);
  792.             }
  793.         }
  794.     } else if (type == GL_INT) {
  795.         GLint *data;
  796.         GLubyte *temp1, *temp2;
  797.         GLint answer;
  798.  
  799.         data = newimage;
  800.         for (i=0; i<imageHeight; i++) {
  801.             for (j=0; j<imageWidth; j++) {
  802.                 for (k=0; k<components; k++) {
  803.                     if (index) {
  804.                         answer = *myimage++ * indexscale;
  805.                     } else {
  806.                         answer = *myimage++ * (GLfloat) 2147482500.0;
  807.                     }
  808.                     if (swapBytes) {
  809.                         temp2 = (GLubyte *) &answer;
  810.                         temp1 = (GLubyte *) data;
  811.                         temp1[0] = temp2[3];
  812.                         temp1[1] = temp2[2];
  813.                         temp1[2] = temp2[1];
  814.                         temp1[3] = temp2[0];
  815.                     } else {
  816.                         data[0] = answer;
  817.                     }
  818.                     data++;
  819.                 }
  820.                 myimage+=(4-components);
  821.             }
  822.         }
  823.     } else if (type == GL_UNSIGNED_INT) {
  824.         GLuint *data;
  825.         GLubyte *temp1, *temp2;
  826.         GLuint answer;
  827.  
  828.         data = newimage;
  829.         for (i=0; i<imageHeight; i++) {
  830.             for (j=0; j<imageWidth; j++) {
  831.                 for (k=0; k<components; k++) {
  832.                     if (index) {
  833.                         answer = *myimage++ * indexscale;
  834.                     } else {
  835.                         answer = *myimage++ * (GLfloat) 4294965000.0;
  836.                     }
  837.                     if (swapBytes) {
  838.                         temp2 = (GLubyte *) &answer;
  839.                         temp1 = (GLubyte *) data;
  840.                         temp1[0] = temp2[3];
  841.                         temp1[1] = temp2[2];
  842.                         temp1[2] = temp2[1];
  843.                         temp1[3] = temp2[0];
  844.                     } else {
  845.                         data[0] = answer;
  846.                     }
  847.                     data++;
  848.                 }
  849.                 myimage+=(4-components);
  850.             }
  851.         }
  852.     } else if (type == GL_FLOAT) {
  853.         GLfloat *data;
  854.         GLubyte *temp1, *temp2;
  855.         GLfloat answer;
  856.  
  857.         data = newimage;
  858.         for (i=0; i<imageHeight; i++) {
  859.             for (j=0; j<imageWidth; j++) {
  860.                 for (k=0; k<components; k++) {
  861.                     if (index) {
  862.                         answer = *myimage++ * indexscale;
  863.                     } else {
  864.                         answer = *myimage++;
  865.                     }
  866.                     if (swapBytes) {
  867.                         temp2 = (GLubyte *) &answer;
  868.                         temp1 = (GLubyte *) data;
  869.                         temp1[0] = temp2[3];
  870.                         temp1[1] = temp2[2];
  871.                         temp1[2] = temp2[1];
  872.                         temp1[3] = temp2[0];
  873.                     } else {
  874.                         data[0] = answer;
  875.                     }
  876.                     data++;
  877.                 }
  878.                 myimage+=(4-components);
  879.             }
  880.         }
  881.     }
  882.     if (alignShift) {
  883.         memcpy((void *) (((int) newimage)+alignShift), newimage, imageSize);
  884.         newimage = (void *) (((int) newimage)+alignShift);
  885.     }
  886. }
  887.  
  888. void changeType(void)
  889. {
  890.     do {
  891.         switch(type) {
  892.           case GL_UNSIGNED_BYTE:
  893.             type = GL_BYTE;
  894.             break;
  895.           case GL_BYTE:
  896.             type = GL_UNSIGNED_SHORT;
  897.             break;
  898.           case GL_UNSIGNED_SHORT:
  899.             type = GL_SHORT;
  900.             break;
  901.           case GL_SHORT:
  902.             type = GL_UNSIGNED_INT;
  903.             break;
  904.           case GL_UNSIGNED_INT:
  905.             type = GL_INT;
  906.             break;
  907.           case GL_INT:
  908.             type = GL_FLOAT;
  909.             break;
  910.           case GL_FLOAT:
  911.             type = GL_BITMAP;
  912.             break;
  913.           case GL_BITMAP:
  914.             type = GL_UNSIGNED_BYTE;
  915.             break;
  916.         }
  917.     } while (!okCombo());
  918.  
  919.     makeNewImage();
  920. }
  921.  
  922. void changeFormat(void)
  923. {
  924.     do {
  925.         switch(format) {
  926.           case GL_RGB:
  927.             format = GL_RGBA;
  928.             break;
  929.           case GL_RGBA:
  930.             format = GL_DEPTH_COMPONENT;
  931.             break;
  932.           case GL_DEPTH_COMPONENT:
  933.             format = GL_COLOR_INDEX;
  934.             break;
  935.           case GL_COLOR_INDEX:
  936.             format = GL_STENCIL_INDEX;
  937.             break;
  938.           case GL_STENCIL_INDEX:
  939.             format = GL_RED;
  940.             break;
  941.           case GL_RED:
  942.             format = GL_GREEN;
  943.             break;
  944.           case GL_GREEN:
  945.             format = GL_BLUE;
  946.             break;
  947.           case GL_BLUE:
  948.             format = GL_ALPHA;
  949.             break;
  950.           case GL_ALPHA:
  951.             format = GL_LUMINANCE;
  952.             break;
  953.           case GL_LUMINANCE:
  954.             format = GL_LUMINANCE_ALPHA;
  955.             break;
  956.           case GL_LUMINANCE_ALPHA:
  957.             format = GL_RGB;
  958.             break;
  959.         }
  960.     } while (!okCombo());
  961.  
  962.     makeNewImage();
  963. }
  964.  
  965. void changeTest(void)
  966. {
  967.     do {
  968.         switch(test) {
  969.           case TEST_DRAW:
  970.             test = TEST_COPY;
  971.             break;
  972.           case TEST_COPY:
  973.             test = TEST_READ;
  974.             break;
  975.           case TEST_READ:
  976.             test = TEST_DRAW;
  977.             break;
  978.         }
  979.     } while (!okCombo());
  980. }
  981.  
  982.  
  983. static void Usage(void)
  984. {
  985.     fprintf(stderr, "Usage: pixtest [-s] [-geometry WxH+X+Y] <filename>\n");
  986.     exit(1);
  987. }
  988.  
  989. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  990. {
  991.     if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
  992.     return GL_TRUE;
  993.     }
  994.     return GL_FALSE;
  995. }
  996.  
  997. int main(int argc, char** argv)
  998. {
  999.     XVisualInfo *vi;
  1000.     Colormap cmap;
  1001.     XSetWindowAttributes swa;
  1002.     XSizeHints sizehints;
  1003.     GLXContext cx;
  1004.     XEvent event;
  1005.     GLboolean needDisplay;
  1006.     XColor white;
  1007.     char *geometry = NULL;
  1008.     char *filename;
  1009.     int i;
  1010.  
  1011.     zoomx = 1.0;
  1012.     zoomy = 1.0;
  1013.     format = GL_RGB;
  1014.     copyFormat = GL_COLOR;
  1015.     type = GL_UNSIGNED_BYTE;
  1016.     filename = NULL;
  1017.     doubleBuffer = GL_TRUE;
  1018.     rgbMode = GL_TRUE;
  1019.     for (i = 1; i < argc; i++) {
  1020.     if (!strcmp(argv[i], "-geometry")) {
  1021.         i++;
  1022.         geometry = argv[i];
  1023.         if (geometry == NULL) {
  1024.         Usage();
  1025.         }
  1026.     } else if (argv[i][0] == '-') {
  1027.             switch (argv[i][1]) {
  1028.           case 's':
  1029.         doubleBuffer = GL_FALSE;
  1030.         break;
  1031.           case 'c':
  1032.         rgbMode = GL_FALSE;
  1033.         break;
  1034.               default:
  1035.                 Usage();
  1036.             }
  1037.         } else {
  1038.         if (filename == NULL) {
  1039.         filename=argv[i];
  1040.         } else {
  1041.         Usage();
  1042.         }
  1043.         }
  1044.     }
  1045.     if (filename == NULL) {
  1046.         Usage();
  1047.     }
  1048.  
  1049.     image = ImageLoad(filename);
  1050.     windowWidth = 4 * image->sizeX;
  1051.     windowHeight = 2 * image->sizeY;
  1052.  
  1053.     dpy = XOpenDisplay(0);
  1054.     if (!dpy) {
  1055.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  1056.     return -1;
  1057.     }
  1058.  
  1059.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  1060.         doubleBuffer ? (rgbMode ? RGB_DB_attributes : CI_DB_attributes) :
  1061.         (rgbMode ? RGB_SB_attributes : CI_SB_attributes));
  1062.     if (!vi) {
  1063.     fprintf(stderr, "No appropriate visual on \"%s\"\n",
  1064.         getenv("DISPLAY"));
  1065.     return -1;
  1066.     }
  1067.  
  1068.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  1069.                rgbMode ? AllocNone : AllocAll);
  1070.     if (rgbMode) {
  1071.     white.red = ~0;
  1072.     white.green = ~0;
  1073.     white.blue = ~0;
  1074.     XAllocColor(dpy, cmap, &white);
  1075.     swa.background_pixel = white.pixel;
  1076.     } else {
  1077.     swa.background_pixel = 15;
  1078.     }
  1079.  
  1080.     if (!rgbMode) {
  1081.     XColor buf;
  1082.     int i;
  1083.  
  1084.     buf.flags = DoRed | DoGreen | DoBlue;
  1085.  
  1086.     /* Init color map */
  1087.     for (i=0; i<16; i++) {
  1088.         buf.pixel = i;
  1089.         buf.blue = (i & 4) ? 65535 : 0;
  1090.         buf.green = (i & 2) ? 65535 : 0;
  1091.         buf.red = (i & 1) ? 65535 : 0;
  1092.         if (i > 8) {
  1093.         buf.red /= 2;
  1094.         buf.green /= 2;
  1095.         buf.blue /= 2;
  1096.         }
  1097.         XStoreColor(dpy, cmap, &buf);
  1098.     }
  1099.     }
  1100.  
  1101.     sizehints.flags = PPosition | PSize;
  1102.     sizehints.width = windowWidth;
  1103.     sizehints.height = windowHeight;
  1104.     sizehints.x = 10;
  1105.     sizehints.y = 10;
  1106.     if(geometry) {
  1107.     int flags, x, y, width, height;
  1108.  
  1109.     flags = XParseGeometry(geometry, &x, &y,
  1110.                    (unsigned int *)&width,
  1111.                    (unsigned int *)&height);
  1112.         if(WidthValue & flags) {
  1113.         sizehints.flags |= USSize;
  1114.         sizehints.width = width;
  1115.         windowWidth = width;
  1116.     }
  1117.     if(HeightValue & flags) {
  1118.         sizehints.flags |= USSize;
  1119.         sizehints.height = height;
  1120.         windowHeight = height;
  1121.     }
  1122.     if(XValue & flags) {
  1123.         if(XNegative & flags)
  1124.         x = DisplayWidth(dpy, DefaultScreen(dpy)) + x 
  1125.             - sizehints.width;
  1126.             sizehints.flags |= USPosition;
  1127.         sizehints.x = x;
  1128.     }
  1129.     if(YValue & flags) {
  1130.         if(YNegative & flags)
  1131.         y = DisplayHeight(dpy, DefaultScreen(dpy)) + y 
  1132.             - sizehints.height;
  1133.             sizehints.flags |= USPosition;
  1134.         sizehints.y = y;
  1135.     }
  1136.     }
  1137.  
  1138.     swa.border_pixel = 0;
  1139.     swa.background_pixel = white.pixel;
  1140.     swa.colormap = cmap;
  1141.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  1142.     | KeyReleaseMask;
  1143.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 
  1144.                sizehints.x, sizehints.y,
  1145.                sizehints.width, sizehints.height,
  1146.                0, vi->depth, InputOutput, vi->visual,
  1147.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  1148.                &swa);
  1149.     XSetStandardProperties(dpy, window, "pixtest", "pixtest", None,
  1150.                            argv, argc, &sizehints);
  1151.     XSetWMColormapWindows(dpy, window, &window, 1);
  1152.     XMapWindow(dpy, window);
  1153.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  1154.  
  1155.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  1156.     if (!glXMakeCurrent(dpy, window, cx)) {
  1157.     fprintf(stderr, "Can't make window current to context\n");
  1158.     return -1;
  1159.     }
  1160.  
  1161.     Init();
  1162.  
  1163.     needDisplay = GL_TRUE;
  1164.     for (;;) {
  1165.     do {
  1166.         XNextEvent(dpy, &event);
  1167.         switch (event.type) {
  1168.           case Expose:
  1169.         needDisplay = GL_TRUE;
  1170.         break;
  1171.           case ConfigureNotify:
  1172.         windowWidth = event.xconfigure.width;
  1173.         windowHeight = event.xconfigure.height;
  1174.         glViewport(0, 0, windowWidth, windowHeight);
  1175.         needDisplay = GL_TRUE;
  1176.         break;
  1177.           case KeyPress:
  1178.         {
  1179.             char buf[100];
  1180.             int rv;
  1181.             KeySym ks;
  1182.             GLboolean setNeed = GL_TRUE;
  1183.  
  1184.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  1185.             switch (ks) {
  1186.               case XK_Escape:
  1187.             exit(0);
  1188.               case XK_I:
  1189.               case XK_i:
  1190.             setNeed = GL_FALSE;
  1191.             glXSwapBuffers(dpy, window);
  1192.             break;
  1193.               case XK_x:
  1194.             i = zoomx / 0.125;
  1195.             i++;
  1196.             zoomx = i / 8.0;
  1197.             break;
  1198.               case XK_X:
  1199.             i = zoomx / 0.125;
  1200.             i--;
  1201.             zoomx = i / 8.0;
  1202.             break;
  1203.               case XK_y:
  1204.             i = zoomy / 0.125;
  1205.             i++;
  1206.             zoomy = i / 8.0;
  1207.             break;
  1208.               case XK_Y:
  1209.             i = zoomy / 0.125;
  1210.             i--;
  1211.             zoomy = i / 8.0;
  1212.             break;
  1213.               case XK_Left:
  1214.             centerx -= 9;
  1215.             break;
  1216.               case XK_Right:
  1217.             centerx += 9;
  1218.             break;
  1219.               case XK_Up:
  1220.             centery += 9;
  1221.             break;
  1222.               case XK_Down:
  1223.             centery -= 9;
  1224.             break;
  1225.               case XK_B:
  1226.               case XK_b:
  1227.             biasing = 1-biasing;
  1228.             if (biasing) {
  1229.                 printf("Biasing on\n");
  1230.             } else {
  1231.                 printf("Biasing off\n");
  1232.             }
  1233.             break;
  1234.               case XK_M:
  1235.               case XK_m:
  1236.             mapping = 1-mapping;
  1237.             if (mapping) {
  1238.                 printf("Mapping on\n");
  1239.             } else {
  1240.                 printf("Mapping off\n");
  1241.             }
  1242.             break;
  1243.               case XK_S:
  1244.               case XK_s:
  1245.             scissor = 1-scissor;
  1246.             if (scissor) {
  1247.                 printf("Scissoring on\n");
  1248.             } else {
  1249.                 printf("Scissoring off\n");
  1250.             }
  1251.             break;
  1252.               case XK_R:
  1253.               case XK_r:
  1254.             changeTest();
  1255.             break;
  1256.               case XK_T:
  1257.               case XK_t:
  1258.             changeType();
  1259.             break;
  1260.               case XK_F:
  1261.               case XK_f:
  1262.             changeFormat();
  1263.             break;
  1264.               case XK_A:
  1265.               case XK_a:
  1266.             alphatst = 1-alphatst;
  1267.             if (alphatst) {
  1268.                 printf("Alpha test on\n");
  1269.             } else {
  1270.                 printf("Alpha test off\n");
  1271.             }
  1272.             break;
  1273.               case XK_E:
  1274.               case XK_e:
  1275.             swapBytes = 1-swapBytes;
  1276.             if (swapBytes) {
  1277.                 glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
  1278.                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  1279.                 glPixelStorei(GL_PACK_SWAP_BYTES, GL_TRUE);
  1280.                 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
  1281.             } else {
  1282.                 glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
  1283.                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
  1284.                 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
  1285.                 glPixelStorei(GL_PACK_LSB_FIRST, GL_TRUE);
  1286.             }
  1287.             makeNewImage();
  1288.             break;
  1289.               case XK_W:
  1290.               case XK_w:
  1291.             alignShift = (alignShift+1)&3;
  1292.             makeNewImage();
  1293.             break;
  1294.               case XK_D:
  1295.               case XK_d:
  1296.             dlisted = 1-dlisted;
  1297.             if (dlisted) {
  1298.                 printf("Display listing\n");
  1299.             } else {
  1300.                 printf("Immediate mode\n");
  1301.             }
  1302.             break;
  1303.               default:
  1304.             setNeed = GL_FALSE;
  1305.             break;
  1306.             }
  1307.             if (setNeed) needDisplay = GL_TRUE;
  1308.         }
  1309.         break;
  1310.         }
  1311.     } while (XPending(dpy) != 0);
  1312.  
  1313.     if (needDisplay) {
  1314.         needDisplay = GL_FALSE;
  1315.         Redraw();
  1316.     }
  1317.     }
  1318. }
  1319.